package org.joe.chinachess.ui;

import lombok.extern.java.Log;
import org.joe.chinachess.core.ChessBoard;
import org.joe.chinachess.core.po.Chess;
import org.joe.chinachess.resources.ResourceLoader;
import org.joe.chinachess.utils.Constants;
import org.joe.chinachess.utils.Point;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Map;

/**
 * 棋盘，以及棋盘上的棋子和事件响应
 *
 * @author Joe
 */

@Log
public class MainPanel extends JPanel {

    private Map<Point, Chess> pointChessMap;// 所有棋子

    private ChessBoard chessboard;

    private NorthPanel northPanel;


    public MainPanel(NorthPanel jPanel) {
        this.setVisible(true);
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                log.info("Start-mousePressed(e = [" + e + "])");
                MainPanel.this.mouseClicked(e);
            }
        });
        this.northPanel = jPanel;
        this.chessboard = new ChessBoard();
        this.pointChessMap = this.chessboard.getPointChessMap();
    }

    /**
     * [CN]画个棋盘
     *
     * @param g
     */
    private void drawChessBoard(Graphics g) {
        int startX = Constants.LINE_START_X;// x坐标
        int startY = Constants.LINE_START_Y;// y坐标

        int endX = Constants.LINE_START_X + (Constants.COLUMN_COUNT - 1)
                * Constants.LINE_SPACE;// 结束的x坐标
        int endY = startY;// 结束点的y坐标

        // 第一步:画横线
        for (int i = 0; i < Constants.ROW_COUNT; i++) {
            g.setColor(Color.RED);
            g.drawLine(startX, startY, endX, endY);
            startY += Constants.LINE_SPACE;
            endY = startY;
        }

        // 第二步：画竖线
//        startX = Constants.LINE_START_X;// x坐标
//        endX = Constants.LINE_START_X;
        startY = Constants.LINE_START_Y;// y坐标
        endY = Constants.LINE_START_Y + (Constants.ROW_COUNT - 1) * Constants.LINE_SPACE;

        // 画上半部分棋盘的中间竖线
        for (int i = 0; i < Constants.COLUMN_COUNT; i++) {
            startX = Constants.LINE_START_X + Constants.LINE_SPACE * i;
            endX = startX;
            g.drawLine(startX, startY, endX, endY);
        }

//        // 画下半部分棋盘的中间竖线
//        startX = Constants.LINE_START_X + Constants.LINE_SPACE;
//        startY = Constants.LINE_START_Y + (Constants.LINE_COUNT)
//                * Constants.LINE_SPACE / 2;
//        endX = startX;
//        endY = Constants.LINE_START_Y + (Constants.LINE_COUNT - 1)
//                * Constants.LINE_SPACE;
//        for (int i = 1; i < Constants.COLUMN_COUNT - 1; i++) {
//            g.drawLine(startX, startY, endX, endY);
//            startX += Constants.LINE_SPACE;
//            endX = startX;
//        }

        // 第三步：画斜线

//        startX = Constants.LINE_START_X + Constants.LINE_SPACE * 3;
//        startY = Constants.LINE_START_Y;
//        endX = startX + Constants.LINE_SPACE * 2;
//        endY = Constants.LINE_START_Y + Constants.LINE_SPACE * 2;
//        g.drawLine(startX, startY, endX, endY);
//        g.drawLine(endX, startY, startX, endY);
//
//        startY += Constants.LINE_SPACE * 7;
//        endY += Constants.LINE_SPACE * 7;
//        g.drawLine(startX, startY, endX, endY);
//        g.drawLine(endX, startY, startX, endY);
    }

    private void drawChess(int x, int y, int chess, int color, int state, Graphics g) {
        g.drawImage(ResourceLoader.getPictures().get(color).get(chess)[state],
                Constants.LINE_START_X + x * Constants.LINE_SPACE,
                Constants.LINE_START_Y + y * Constants.LINE_SPACE,
                Constants.LINE_SPACE,
                Constants.LINE_SPACE,
                null);
    }

    private void drawAllChesses(Graphics g) {
        for (Map.Entry<Point, Chess> pointChessEntry : pointChessMap.entrySet()) {
            Point point = pointChessEntry.getKey();
            Chess chess = pointChessEntry.getValue();
            if (chess == null) {
                continue;
            }
            drawChess(point.getX(), point.getY(), chess.getChess(), chess.getColor(), chess.getState(), g);

        }
    }

    public void drawChatWindow(Graphics g) {
        g.setColor(Color.red);

        int ox = Constants.CHAT_LINE_START_X;
        int oy = Constants.CHAT_LINE_START_Y;
        int dx = ox + Constants.CHAT_PANEL_WIDTH;
        int dy = oy + Constants.CHAT_PANEL_HEIGHT;

        g.drawLine(ox, oy, ox, dy);
        g.drawLine(ox, oy, dx, oy);
        g.drawLine(dx, dy, dx, oy);
        g.drawLine(dx, dy, ox, dy);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        drawChessBoard(g);
        drawAllChesses(g);
        //this.drawChatWindow(g);
    }

    /**
     * 根据坐标的实际位置，求坐标对应的数组位置
     *
     * @param x
     * @param y
     * @return
     */
    private Point getLogicLocation(int x, int y) {

        int sx = Constants.LINE_START_X;// 表示(0,0)的实际坐标范围
        int sy = Constants.LINE_START_Y;
        int ex = Constants.LINE_START_X + (Constants.COLUMN_COUNT - 1) * Constants.LINE_SPACE;
        int ey = Constants.LINE_START_Y + (Constants.ROW_COUNT - 1) * Constants.LINE_SPACE;

        if (x < sx || x > ex || y < sy || y > ey) {
            return null;
        }

        int m = (x - sx) / Constants.LINE_SPACE;
        int n = (y - sy) / Constants.LINE_SPACE;
        return new Point(m, n);
    }


    private void mouseClicked(MouseEvent e) {

        Point p = this.getLogicLocation(e.getX(), e.getY());// 取得逻辑位置
        if (p == null) {// 在棋盘范围外
            return;
        }
        log.info(String.format("位置：x = %d, y = %d ", p.getX(), p.getY()));
        this.chessboard.operation(p);
        northPanel.updateText(this.chessboard.getAColor(), this.chessboard.getBColor(), this.chessboard.getStep());
        this.repaint();
    }


}
